layui.use(['table','form','laydate', 'element'], function () {
    let table = layui.table,
        form = layui.form,
        $ = layui.jquery,
     laydate = layui.laydate,
        element = layui.element;
    laydate.render({
        elem: '#executionTime'
        ,type: 'datetime'
        ,range: true
        ,done: function(value, date){
            const start = value.substring(0,20);
            const end = value.substring(22,44);
            $("#beginTime").val(start)
            $("#endTime").val(end)
            // layer.alert('你选择的日期是：' + value + '<br>获得的对象是' + JSON.stringify(date));
        }
    });
    //方法级渲染
    table.render({
        elem: '#jobLog'
        , url: '/monitor/jobLog/list'
        , toolbar: '#toolbarTable'
        , title: '定时任务'
        , totalRow: true
        ,skin: 'line'
        , id: "jobLog"
        ,where: {
            jobName: $('#jobName').val(),
            beginTime: $('#beginTime').val(),
            endTime: $('#endTime').val(),
        }
        , cols: [[
            {checkbox: true, fixed: true}
            , {field: 'jobLogId', title: '日志编号', width: 150, sort: true, align: 'center'}
            , {field: 'jobName', title: '任务名称', width: 250, sort: true}
            , {field: 'invokeTarget', title: '调用目标字符串', width: 300, sort: true}
            , {field: 'jobMessage', title: '日志信息', width: 500, sort: true}
            , {field: 'status', title: '状态', width: 100, sort: true,templet: function (d) {
                    let className = '';
                    let name=""
                    if (d.status == '0'){
                        className = 'layui-bg-green'
                        name="成功"
                    }else {
                        className = 'layui-bg-red'
                        name="失败"
                    }
                    return '<span class = "layui-badge '+className+'" >'+name+'</span>';
                }}
            , {field:'createTime', title:'执行时间', width:200, sort: true}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 100}
        ]]
        , even: true
        , page: true //是否显示分页
        , skip: true //开启跳页
        , limits: [10, 30, 60]
        , width: 1700
        // , height: 'full-150'
        , limit: 10 //每页默认显示的数量
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            return {
                "code": status, //解析接口状态   （好像必须是0 才可以）
                "msg": data.desc, //解析提示文本
                "count": data.total, //解析数据长度
                "data": data.records //解析数据列表
            };
        }
        ,done:function (res,currentCount) {
            //***重点***：table渲染完成后渲染element进度条
            element.render()
        }

    });


    //头工具栏事件
    table.on('toolbar(jobLog)', function (obj) {
        let data = table.checkStatus(obj.config.id).data;
        switch (obj.event) {
            case 'del':
                if (data.length==0){
                    layer.msg("请先勾选数据",{icon:2,time:3000});
                    return;
                }
                let ids = "";
                for (let i = 0; i < data.length; i++) {
                    ids+=data[i].jobLogId+","
                }
                ids = ids.substr(0,ids.length-1);
                del(ids);
                break;
            case 'clean':
                clean();
                break;
        }
        ;
    });
    table.on('tool(jobLog)', function (obj) {
        const data = obj.data;
        if (obj.event === 'info'){
            info(data)
        }
    });
    function info(data) {
        layer.open({
            type: 2,
            title: '日志详情',
            area: ['850px', '700px'],
            fix: false, //
            closeBtn: false,
            shift: 2,
            shadeClose: true,
            content: ["info.html", 'no'],
            success: function (layero, index) {
                const body = layer.getChildFrame('body', index);//获取弹出层的dom元素
                body.find("#jobLogId").val(data.jobLogId);
            },
            yes: function () {
            }
        });
    }
    function clean(){
        $.ajax({
            url: '/monitor/jobLog/clean',
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            type: "delete",
            success: function (result) {
                layer.msg(result.desc, {icon: 1, time: 1000}, function () {
                    location.reload();
                })
            }
        })
    }

    function del(ids) {
        layer.confirm("确认要删除吗，删除后不能恢复", { title: "删除确认" }, function (index) {
            $.ajax({
                type: "delete",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "/monitor/jobLog/"+ids,//url
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg(result.desc,{icon:1,time:1000},function () {
                            location.reload();
                        });
                    };
                },
                error: function () {
                    alert("异常！");
                }
            });
        })
    }


    $('#query').click(function(){
        search();
    });
    $(document).keyup(function(event){
        if(event.keyCode ==13){
            search();
        }
    });
    function search(){
        const content = $('#content');
        //执行重载
        table.reloadData('jobLog', {
            method: 'get',
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                jobName: $('#jobName').val(),
                status: $('#status').val(),
                beginTime: $('#beginTime').val(),
                endTime: $('#endTime').val(),
            }
        }, 'data');
    }
});